<?
	class c_table_controller extends c_base_controller
	{
		function index()
		{
			global $mvc;
			$this->parse_request();
			return $this->show('index');
		}
		
		// Разбирает данные запроса и заполняет нужные поля.
		function parse_request()
		{
			$this->data['id'] = isset($_REQUEST['id']) ? (int)$_REQUEST['id'] : '';
			$this->data['name'] = isset($_REQUEST['name']) ? $_REQUEST['name'] : '';		
			$this->data['ffilter'] = isset($_REQUEST['ffilter']) ? $_REQUEST['ffilter'] : '';
			//$this->data['page'] = (isset($_REQUEST['page']) ? (int)$_REQUEST['page'] : 1);			
			
			$this->data['name'] = strtolower($this->data['name']);
		}
		
		function edit()
		{
			global $mvc;
			$this->parse_request();
			$this->table = new c_table_model($this->data['name']);
			return $this->show('edit');
		}
		
		function save()
		{
			global $mvc;
			$this->parse_request();
			$this->table = new c_table_model($this->data['name']);
			
			$cnt = (int)$_REQUEST['stor_fcount'];
			$this->table->name = mysql_escape_string($_REQUEST['name']);
			$this->table->comment = mysql_escape_string($_REQUEST['desc']);
			$this->table->fields = array();

			for($i = 0; $i < $cnt; $i++)
			{
				$field = array();
				$field['name'] = $_REQUEST['stor_f'.$i.'N'];
				$field['type'] = $_REQUEST['stor_f'.$i.'T'];
				$field['len'] = $_REQUEST['stor_f'.$i.'L'];
				$field['comment'] = $_REQUEST['stor_f'.$i.'C'];

				$this->table->fields[$field['name']] = $field;
			}
			$this->table->save();
			
			return $this->show('index');
		}
		
		function delete()
		{
			global $mvc;
			$this->parse_request();
			$mvc->db->query('DROP TABLE '.$this->data['name'], false);
			
			return $this->show('index');
		}		
	}
?>